// zhiboDetail.js
var app = getApp()

Page({
  data:{
    PageInterface : app.globalData.domain + app.globalData.PageInterface
  },
  onLoad: function (option) {
    var thiz = this
    var PageInterface = app.globalData.domain + app.globalData.PageInterface
    var domain = app.globalData.domain

    thiz.add_man({
      curr_id:option.curr_id,
      success:function(res){
        wx.showToast({
          title: res.wait,
          icon:'loading',
          duration:1000
        })
      }
    })

    wx.getSystemInfo({
      success: function (res) {
        thiz.setData({
          domain: domain,
          hiddenLoading: false,
          windowHeight: res.windowHeight,
          windowWidth: res.windowWidth
        });
      }
    })

    wx.request({
      url: PageInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Curr',
        api: 3001,
        curr_id: option.curr_id
      },
      success: function (res) {
        if (res.data.status == 1) {
          if (res.data.data) {
            res.data.data.curr_time = toDate(res.data.data.curr_time)
          }
          thiz.setData({
            zhibo: res.data.data,
            hiddenLoading: true
          })
        }
      }
    })
    //商品列表
    wx.request({
      url: PageInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Goods',
        api: 4003,
        curr_id: option.curr_id
      },
      success: function (res) {
        if (res.data.data.length > 0) {
          res.data.data.forEach(function (element, index) {
            element.default_img = domain + element.default_img
          })
          thiz.setData({
            shangpins: res.data.data
          })
        }
      }
    })

    function toDate(number) {
      var n = number * 1000;
      var date = new Date(n);
      var Y = date.getFullYear() + '/';
      var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '/';
      var D = date.getDate() < 10 ? '0' + date.getDate() : date.getDate();
      return (Y + M + D)
    }
  },

  //观看人数增加
  add_man : function (option){
    var thiz = this;
    wx.request({
      url: thiz.data.PageInterface,
      method: 'POST',
      header: { 'content-type': 'application/x-www-form-urlencoded' },
      data: {
        format_type: 'json',
        api_cate: 'Curr',
        api: 3003,
        curr_id: option.curr_id
      },
      success: function (res) {
        option.success && option.success({
          wait:'请稍后'
        })
      }
    })
  }
})